package mil.af.amc.adpetracker.web.actions.struts1.DrmoPackage;

import mil.af.amc.adpetracker.web.actions.struts1.AbstractAction;
import mil.af.amc.adpetracker.service.DrmoPackageService;
import mil.af.amc.adpetracker.service.ServiceException;
import org.apache.struts.action.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Action for creating a new DRMO Package
 *
 * @author TSgt Jason Ferguson
 */

public class CreateDrmoPackageAction extends AbstractAction {

    private Log log = LogFactory.getLog(CreateDrmoPackageAction.class);

    private DrmoPackageService drmoPackageService;

    public void setDrmoPackageService(DrmoPackageService drmoPackageService) {
        this.drmoPackageService = drmoPackageService;
    }

    @Override
    public ActionForward execute(ActionMapping actionMapping,
                                 ActionForm actionForm,
                                 HttpServletRequest httpServletRequest,
                                 HttpServletResponse httpServletResponse) throws Exception {

        DynaActionForm form = (DynaActionForm) actionForm;

        log.trace("CreateDrmoPackageAction: creating DRMO Package for " + form.get("dtid"));
        try {
            drmoPackageService.createPackage(form.getMap());
        } catch (ServiceException e) {

            ActionMessage message = new ActionMessage(e.getKey());
            saveError(httpServletRequest, message);

            return actionMapping.getInputForward();
        }

        return actionMapping.findForward("success");

    }
}
